package com.keter.framework.mail;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Component
public class KeterMail {

    /**
     * 这逼玩意儿找不到配置文件也报未声明Bean的错！
     */
    @Autowired
    JavaMailSenderImpl mailSender;

    // 仅用于单体测试环境验证
    public void haha(){

    }

    // 只有收件人(to)
    public void sendMail(String title, String html, List<String> to) throws Exception {
        sendMail(title, html, to, new ArrayList<>(), new ArrayList<>());
    }

    // to cc
    public void sendMail(String title, String html, List<String> to, List<String> cc) throws Exception {
        sendMail(title, html, to, cc, new ArrayList<>());
    }

    // to cc bcc
    public void sendMail(String title, String html, List<String> to, List<String> cc, List<String> bcc) throws Exception {
        sendMail(title, html, to, cc, bcc, null);
    }

    // to cc bcc files
    public void sendMail(String title, String text, List<String> to, List<String> cc, List<String> bcc, List<File> files) throws Exception {
        if (to == null || to.size() == 0) {
            throw new Exception("邮件发送失败：收件人列表为空！");
        }
        Multipart multipart = new MimeMultipart();
        try {
            MimeMessage msg = makeMessageWithFiles(title, text, to, cc, bcc, files, multipart, mailSender);
            mailSender.send(msg);
            System.out.println("邮件发送成功！");

        } catch (Exception e) {
            throw new Exception("邮件发送失败", e);
        }
    }

    private static MimeMessage makeMessageWithFiles(
            String title, String text, List<String> to, List<String> cc, List<String> bcc, List<File> files,
            Multipart multipart, JavaMailSender mailSender) throws MessagingException, UnsupportedEncodingException {
        MimeMessage msg = mailSender.createMimeMessage();
        msg.setSubject(title);
        msg.setText(text);

        for (String string : to) {
            if (StringUtils.isNotEmpty(string)) {
                msg.addRecipients(RecipientType.TO, string);
            }
        }

        if (cc != null) {
            for (String string : cc) {
                if (StringUtils.isNotEmpty(string)) {
                    msg.addRecipients(RecipientType.CC, string);
                }
            }
        }

        if (bcc != null) {
            for (String string : bcc) {
                if (StringUtils.isNotEmpty(string)) {
                    msg.addRecipients(RecipientType.BCC, string);
                }
            }
        }

        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(text, "text/html; charset=utf-8");
        multipart.addBodyPart(html);
        msg.setContent(multipart);

        // 添加附件的内容
        if (files != null && files.size() > 0) {
            for (File attachment : files) {
                BodyPart attachmentBodyPart = new MimeBodyPart();
                DataSource source = new FileDataSource(attachment);
                attachmentBodyPart.setDataHandler(new DataHandler(source));
                //MimeUtility.encodeWord可以避免文件名乱码
                attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
                multipart.addBodyPart(attachmentBodyPart);
            }
            msg.setContent(multipart);
        }
        return msg;
    }
}